package JAVACORE_TRAINING.OOPS_CONCEPTS.ARRAY_AND_COLLECTIONS.STATIC_ARRAYS.EXCERCISES.EX06;

public class Models {
    /**
     * Nghiệp vụ sắp xếp thông tin sinh viên theo điểm
     * @param objStudent
     */
    public StudentObject[] StudentSortOfSelection(StudentObject[] objStudent) {
    	StudentObject[] objTemp = 
                            new StudentObject[objStudent.length];
        System.arraycopy(objStudent, 0, objTemp, 0, objStudent.length);

        for (int i = objTemp.length - 1; i >= 1; i--) {
            // Find the maximum in the list[0..i]
        	StudentObject currentScores = objTemp[0];
            int currentMaxIndex = 0;
            {
                for (int j = 1; j <= i; j++) {
                    if (currentScores.getScores() > objTemp[j].getScores()) {
                        currentScores = objTemp[j];
                        currentMaxIndex = j;
                    }
                }
                // Swap list[i] with list[currentMaxIndex] if necessary;
                if (currentMaxIndex != i) {
                    objStudent[currentMaxIndex] = objTemp[i];
                    objTemp[i] = currentScores;
                }
            }
        }
        return objTemp;
    }

    // Sort - Insertion Sort?
    public StudentObject[] StudentSortOfInsertionSort(StudentObject[] objStudent) {
    	StudentObject[] objTemp = 
                                new StudentObject[objStudent.length];
        System.arraycopy(objStudent, 0, objTemp, 0, objStudent.length);
        
        for (int i = 1; i < objTemp.length; i++) {
        	StudentObject currentElement = objTemp[i];
            int k;
            for (k = i - 1; k >= 0 && objTemp[k].getScores() > currentElement.getScores(); k--) {
                objTemp[k + 1] = objTemp[k];
            }
            // Insert the current element into list[k+1]
            objTemp[k + 1] = currentElement;
        }
        return objTemp;
    }

    // Sort - Buggdy Sort?
    /***
     * Nghiệp vụ sắp xếp thông tin sinh viên theo điểm với Buggdy Sort
     * @param objStudent
     */
    public StudentObject[] StudentBuggdySort(StudentObject[] objStudent) {
    	StudentObject[] objTemp = 
                new StudentObject[objStudent.length];
        System.arraycopy(objStudent, 0, objTemp, 0, objStudent.length);
        for (int i = 0; i < objTemp.length; i++) {
            for (int j = i + 1; j < objTemp.length; j++) {
                if (objTemp[i].getScores() > objTemp[j].getScores()) {
                	StudentObject temp = objTemp[i];
                    objTemp[i] = objTemp[j];
                    objTemp[j] = temp;
                }
            }
        }
        return objTemp;
    }
}
